package main

import (
	"log"
	"strings"

	"github.com/libp2p/go-libp2p"
	"github.com/libp2p/go-libp2p/core/crypto"
	"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
)

func main() {

	// 创建自己的私钥，并且指定监听模式以及端口生成libp2p节点
	keyReader := strings.NewReader("00000000000000000000000000000000")

	privateKey, _, err := crypto.GenerateEd25519Key(keyReader)

	if err != nil {
		panic(err)
	}

	if err != nil {
		panic(err)
	}

	relayNode, err := libp2p.New(
		//使用私钥
		libp2p.Identity(privateKey),
		//指定监听端口
		libp2p.ListenAddrStrings(
			"/ip4/0.0.0.0/tcp/8520",
			"/ip4/0.0.0.0/udp/8521/quic-v1",
		),
	)

	if err != nil {
		panic(err)
	}

	defer relayNode.Close()

	_, err = relay.New(relayNode)
	if err != nil {
		panic(err)
	}

	log.Printf("服务端ID: %s", relayNode.ID())
	log.Printf("监听的地址: %v", relayNode.Addrs())

	select {}
}
